package com.woniu.flowable.controller;


import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RequestMapping
@RestController
public class FlowableController {
    @Autowired
    private ProcessEngine processEngine;

    @PutMapping("creatFlowable")
    public void creatFlowable(String key,String businessKey,HashMap variables){
        //获取启动流程的实例
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //构建流程携带的变量信息。
        variables.put("employee","张三") ;// 谁申请请假
        variables.put("nrOfHolidays",3); // 请几天假
        variables.put("description","工作累了，想出去玩玩"); // 请假的原因
        variables.put("flowableKey",key);//此流程的key值
        //使用key启动流程，也可使用id等
        ProcessInstance goodsFlowable = runtimeService.startProcessInstanceById("goodsFlowable",businessKey,variables);
        //输出相关的流程实例信息
        System.out.println("流程定义的ID：" + goodsFlowable.getProcessDefinitionId());
        System.out.println("流程实例的ID：" + goodsFlowable.getId());
        System.out.println("当前活动的ID：" + goodsFlowable.getActivityId());

    }
    /*
    * 获取流程携带的信息
    * */
    @PutMapping("getMsg")
    public List<Map<String, Object>> getMsg(String key, String username) {
        //获取流程引擎对象
        TaskService taskService = processEngine.getTaskService();
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(key)//指定处理流程名称
                .taskAssignee(username)//指定处理人
                .list();//获取由user处理的所有流程
        List<Map<String, Object>> collect = list.stream().map(e -> {
            Map<String, Object> processVariables = e.getProcessVariables();
            processVariables.put("InstanceId",e.getProcessInstanceId());
            processVariables.put("id",e.getId());
            return processVariables;
        }).collect(Collectors.toList());
        return collect;
    }
    @PutMapping("examine")
    public Integer examine(String InstanceId, String username,Boolean variable ){
        //获取流程引擎对象
        TaskService taskService = processEngine.getTaskService();
        Task task1 = taskService.createTaskQuery()
                .processInstanceId(InstanceId)//制定处理流程id
                .taskAssignee(username)//指定处理人
                .singleResult();//获取由user处理的所有流程
        /*获取并打印流程变量携带信息*/
        Map<String, Object> processVariables = task1.getProcessVariables();
        Set<String> keys = processVariables.keySet();
        for (String key : keys) {
            Object o = processVariables.get(key);
            System.out.println("o = " + o);
        }
        Map<String,Object> variables = new HashMap<>();
            variables.put("approved",variable); // 同意请求
        if (variable){

            taskService.complete(task1.getId(),variables);
            return 1;
        }else {
            taskService.complete(task1.getId(),variables);
            return 0;
        }
    }
}
